package org.my.stock.util;

import java.beans.PropertyVetoException;
import java.sql.*;

public class MyStockDawnSqlUtil {
    public static Connection getConnection(int num) {
        Connection conn = null;
        try {
            Class.forName("org.apache.ignite.IgniteJdbcDriver");
            String url = "jdbc:ignite:thin://localhost:10800/public?lazy=true&userToken=dafu";
            conn = DriverManager.getConnection(url);
        } catch (Exception ex) {
            if (num > 0) {
                return getConnection(num - 1);
            }
        }

        return conn;
    }

    public static Connection getConnection() {
        return getConnection(5);
    }

    public static int has_type(final Connection conn, final String type_name) throws SQLException, ClassNotFoundException {
        //Connection conn = getConnection();
        int id = 0;
        try {
            PreparedStatement statement = conn.prepareStatement("select m.id as id from public.my_type as m where m.my_type_name = ?");
            statement.setString(1, type_name);
            ResultSet rs = statement.executeQuery();
            while (rs.next())
            {
                id = rs.getInt(1);
                break;
            }
            rs.close();
            statement.close();
        }
        catch (Exception e)
        {
            throw e;
        }

        return id;
    }

    /**
     * 判断股票是否已经存在股票表中了
     * */
    public static boolean has_stock(final String symbol, final String name, final String type_name) throws PropertyVetoException, SQLException, ClassNotFoundException {
        Connection conn = getConnection();
        boolean flag = false;
        try {
            if (conn == null) {
                conn = getConnection();
            }
            PreparedStatement statement = conn.prepareStatement("select m.symbol as symbol from public.my_stock as m where m.symbol = ?");
            statement.setString(1, symbol);
            ResultSet rs = statement.executeQuery();
            while (rs.next())
            {
                flag = true;
                break;
            }
            rs.close();
            statement.close();

            if (flag == false)
            {
                PreparedStatement st = conn.prepareStatement("insert into public.my_stock (symbol, type_id, my_stock_name) values (?, ?, ?)");
                st.setString(1, symbol);
                st.setInt(2, MyStockDawnSqlUtil.has_type(conn, type_name));
                st.setString(3, name);
                st.executeUpdate();

                st.close();
                flag = true;
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally {
            conn.close();
        }

        return flag;
    }

    /**
     * 判断股票是否已经存在股票表中了
     * */
    public static void has_stock_type(final String symbol, final String type_name) throws PropertyVetoException, SQLException, ClassNotFoundException {
        Connection conn = MyUtil.getConnection();
        boolean flag = false;
        try {
            PreparedStatement statement = conn.prepareStatement("select m.id as id from my_stock_type as m where m.symbol = ? and m.type_name = ?");
            statement.setString(1, symbol);
            statement.setString(2, type_name);
            ResultSet rs = statement.executeQuery();
            while (rs.next())
            {
                flag = true;
                break;
            }

            if (flag == false)
            {
                PreparedStatement st = conn.prepareStatement("insert into my_stock_type (symbol, type_name) values (?, ?)");
                st.setString(1, symbol);
                st.setString(2, type_name);
                st.executeUpdate();
            }
        }
        catch (Exception e)
        {
            throw e;
        }
        finally {
            conn.close();
        }
    }

//    /**
//     * 添加股票和股票分类
//     * */
//    public static void insert_stock(final String symbol, final String name, final String type_name) throws PropertyVetoException, SQLException, ClassNotFoundException {
//
//        if (has_stock(symbol, name, type_name) == false)
//        {
//            has_stock_type(symbol, type_name);
//        }
//    }
}
